<?php
/**
 * 功能描述：	通用文章管理
 * @datetime	2022-07-12
 * @version   	v2.3
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_EssayController extends Controller {

    private $CLASS_NAME;
    private $CLASS_FUNS;

    // 是否登录
    function __construct($controller, $action) {
        parent::__construct($controller, $action);
        $loginSer = new Admin_LoginServe();
        $loginSer->judgeSessionLogin($controller, $action);
        $this->CLASS_NAME = get_class($this);
        $this->CLASS_FUNS = get_class_methods($this);
        /*$mould = Fun::input('m');
        if($this->CLASS_NAME){
            $classNameArr = explode('_',$this->CLASS_NAME);
            $classPrefix = isset($classNameArr[0]) ? $classNameArr[0] : '';
            $this->CLASS_NAME = $classPrefix.'_'.$mould.'Controller';
        }*/
    }

    /**
     * Notes: 文章列表主页面
     * User: ZhuHaili
     * Date: 2022/8/27
     */
    public function index(){
        $mouldCode = Fun::input('m');
        $this->lists($mouldCode, 'index');
    }

    /**
     * Notes:文章列表
     * User: ZHL
     * Date: 2019/9/18
     */
    public function lists($mouldCode, $temp, $orderby = 'a.viewseq desc,a.createdate desc,a.id desc'){
        // 接收参数
        $menu_id = Fun::input('c', 0, 'd');
        $pageNo = Fun::input('p', 1, 'd');
        $fid = Fun::input('f', 0, 'd');
        $findLevel = Fun::input('l', 0, 'd');
        $search = isset($_POST['search']) ? json_decode($_POST['search'], true) : array();
        // 获取模型内容
        $commM = new Admin_CommonModel();
        $mouldInfo = $commM->getEssayInfo('sysmould',array('mouldcode'=>$mouldCode));
        // 文章列表
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->getEssayList($mouldCode, $menu_id, $pageNo, $fid, $search, $orderby);
        if(!is_array($result)){
            htmlAlert($result);
        }
        // 主表关联字段
        $fieldSer = new Admin_FieldServe();
        $fields1 = $fieldSer->getOpenClassFieldsInfos($menu_id,'2','1');
        // 多菜单关联菜单列表
        $menuSer = new Admin_MenuServe();
        $classInfo = $menuSer->getMenuInfo(array('id'=>$menu_id));
        if(empty($classInfo)){ htmlAlert('当前菜单已经不存在'); }
        $classMeet = $menuSer->getClassList($classInfo['navtype']);
        // 用户权限信息
        $powerSer = new Admin_PowerServe();
        $powerInfo = $powerSer->getUserPowerInfo();
        // 子父层级控制
        if(!$findLevel){
            $findLevel = intval($classInfo['findflag']) ? 1 : 0;
        }else{
            $findLevel = intval($classInfo['findflag']) > $findLevel ? $findLevel+1 : 0;
        }
        // 映射
        $this->set('powerInfo', $powerInfo);
        $this->set('statusList', BaseData::$statusArr2);
        $this->set('fieldFileType', array_keys(BaseData::$fieldTypeArrFile));
        $this->set('fieldPicType', array_keys(BaseData::$fieldTypeArrPic));
        $this->set('fieldBtnType', array_keys(BaseData::$fieldTypeArrBtn));
        $this->set('mouldCode', $mouldCode);
        $this->set('mouldInfo', $mouldInfo);
        $this->set('menu_id', $menu_id);
        $this->set('fid', $fid);
        $this->set('findLevel', $findLevel);
        $this->set('userLevel', getUserLevel());
        $this->set('fields', $fields1);
        $this->set('classMeet', $classMeet);
        $this->set('classInfo', $classInfo);
        $this->set('searchInfo', $result['where']);
        $this->set('list', $result['list']);
        $this->set('pages', $result['pages']);
        $this->set('title', '列表');
        $this->display($temp);
    }

    /**
     * Notes:文章导出Execl表格
     * User: ZhuHaili
     */
    function execl(){
        $mouldCode = Fun::input('m');
        $menu_id = Fun::input('c', 0, 'd');
        $pageNo = Fun::input('p', 0, 'd');
        $fid = Fun::input('f', 0, 'd');
        $idStr = Fun::input('i');
        $search = Fun::input('search');
        $orderby = Fun::input('o'); //排序例如：a.id_desc,a.viewseq_desc
        if($orderby){$orderby = strtr($orderby, '_', " ");} // 替换字符串
        $menuSer = new Admin_MenuServe();
        $classInfo = $menuSer->getMenuInfo(array('id'=>$menu_id));
        if(empty($classInfo)){
            htmlAlert('网络异常稍后再试');
        }
        // 获取数据
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->getEssayLists($mouldCode, $menu_id, $pageNo, $fid, $idStr, $search, $orderby);
        // 导出execl
        //$fileNameStr = '_page'.$result['page'];
        $fileNameStr = '';
        $fileTitle = $classInfo['menuname'].$fileNameStr.'_'.date('Ymd-His'); // execl文件名
        $e = array('id','title'); // 屏蔽科学计数法字段
        $csv = new ManageCSV();
        $csv->export($result['trList'], $result['thList'], $fileTitle, $e);
    }

    /**
     * Notes:文章添加
     * User: ZhuHaili
     */
    public function add(){
        $this->setEssay('add', 'add');
    }

    /**
     * Notes:文章修改
     * User: ZhuHaili
     */
    public function update(){
        $this->setEssay('update', 'update');
    }

    /**
     * Notes:文章添加 / 修改
     * User: ZhuHaili
     * Date: 2022/8/10
     * @param $param
     * @param $tmpl
     */
    public function setEssay($setFlag, $tmpl){
        $param = Fun::input();
        $subFlag = Fun::input('subFlag','');  // 数据来源标识
        $essaySer = new Admin_EssayServe();
        //## 来自提交数据时
        if($subFlag == 'Y'){
            $foreachNo = isset($param['foreachno']) && intval($param['foreachno']) ? intval($param['foreachno']) : 1; // 循环添加次数
            $title = isset($param['title']) ? $param['title'] : '';
            for($i = 0; $i < $foreachNo; $i ++){
                if($title){
                    $param['title'] = $i ? ($i+1).$title : $title;
                }
                $result = $essaySer->setEssay($param, $setFlag);
            }
            Fun::json($result, true);
        }
        //## 来自页面预览时
        $setData['mouldCode'] = Fun::input('m','');    // 模型编码
        $setData['menu_id'] = Fun::input('c','');      // 菜单id
        $setData['fid'] = Fun::input('f','');          // 父级ID
        $setData['essay_id'] = Fun::input('i','');     // 文章id
        $setData['info'] = array();
        $commModel = new Admin_CommonModel();
        $mouldInfo = $commModel->getEssayInfo('sysmould', array('mouldcode'=>$setData['mouldCode']));
        // 修改操作时
        if($setFlag == 'update'){
            // 当前文章详情
            $setData['info'] = $commModel->getEssayInfos($setData['mouldCode'], array('a.id'=>$setData['essay_id']));
            if(empty($setData['info'])){
                htmlAlert('当前内容已经不存在');
            }
            // 已关联的栏目
            if($mouldInfo['classflag'] == 1){
                $setData['essayIdClassId'] = $essaySer->getEssayIdMenuId($setData['mouldCode'], $setData['essay_id']);
            }
        }
        // 多菜单关联菜单列表
        $setData['menuInfo'] = $classInfo = $commModel->getEssayInfo('sysmenu', array('id'=>$setData['menu_id']));
        if(empty($classInfo)){ htmlAlert('当前菜单已经不存在'); }
        $classSer = new Admin_MenuServe();
        $setData['classMeet'] = $classSer->getClassList($classInfo['navtype']);

        // 文章开放字段
        $fieldSer = new Admin_FieldServe();
        $mainFields = $fieldSer->getOpenClassFieldsInfos($setData['menu_id'], '2', '1');
        $viceFields = $fieldSer->getOpenClassFieldsInfos($setData['menu_id'], '2', '2');
        $fields = array_merge($mainFields, $viceFields);
        $fieldItems = FieldItemAdd::setItems($fields, $setData['info']);
        $setData['fieldItemHtml'] = isset($fieldItems['html']) ? $fieldItems['html'] : '';
        $setData['fieldItemStyle'] = isset($fieldItems['style']) ? $fieldItems['style'] : '';
        // 映射
        foreach($setData as $k => $v){
            $this->set($k, $v);
        }
        $this->set('title', $setData['mouldCode'].'_'.$setData['menu_id'].'_'.$setData['essay_id']);
        $this->display($tmpl);
    }

    /**
     * Notes:文章删除
     * User: ZHL
     * Date: 2022/7/18
     */
    public function del(){
        $mould = Fun::input('m');
        $menu_id = Fun::input('c');
        $id = Fun::input('i');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->delEssay($mould, $menu_id, $id);
        Fun::json($result, true);
    }

    /**
     * Notes:文章批量删除
     * User: ZHL
     * Date: 2022/7/18
     */
    public function dels(){
        $mould = Fun::input('m');
        $menu_id = Fun::input('c');
        $idStr = Fun::input('i');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->delEssayMore($mould, $menu_id, $idStr);
        Fun::json($result, true);
    }

    /**
     * Notes:文章批量移动
     * User: ZHL
     * Date: 2022/7/18
     */
    public function move(){
        $mould = Fun::input('m');
        $menu_id = Fun::input('c');
        $newClassId = Fun::input('c2');
        $idStr = Fun::input('i');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->moveEssayMore($mould, $menu_id, $newClassId, $idStr);
        Fun::json($result, true);
    }

    /**
     * Notes: 文章批量排序
     * User: ZHL
     * Date: 2022/7/18
     */
    public function seq(){
        $mould = Fun::input('m');
        $param = Fun::input('param');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->seqEssay($mould, $param);
        Fun::json($result, true);
    }

    /**
     * Notes:批量修改状态
     * User: ZhuHaili
     */
    public function status(){
        $mould = Fun::input('m');
        $menu_id = Fun::input('c');
        $newStatus = Fun::input('s');
        $idStr = Fun::input('i');
        $essaySer = new Admin_EssayServe();
        $result = $essaySer->updateEssayStatusMore($mould, $menu_id, $newStatus, $idStr);
        Fun::json($result, true);
    }

    //////////////////////////////////////////////////////////////////////////
    ///
    //////////////////////////////////////////////////////////////////////////

    /**
     * Notes:文章属性列表
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    public function elementList(){
        $mouldCode = Fun::input('m');
        $essayId = Fun::input('i');
        $essayField = Fun::input('f');
        $pageNo = Fun::input('p',1,'d');
        $essaySer = new Admin_EssayServe();
        $list = $essaySer->getEssayElementList($mouldCode, $essayId, $essayField, $pageNo);
        $this->set('list', $list);
        $this->display();
    }

    /**
     * Notes:文章属性删除
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    public function elementDel(){
        $mouldCode = Fun::input('m');
        $lid = Fun::input('lid');
        if(!$mouldCode || !intval($lid)){
            Fun::json('非法请求_未指定标识', true);
        }
        $where['lid'] = intval($lid);
        $commM = new Admin_CommonServe();
        $commM->delWhere($mouldCode.'_list', $where);
        Fun::json('Y', true);
    }

    /**
     * Notes:文章属性添加
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    public function elementAdd(){
        $param = Fun::input();
        $this->elementSet($param, 'elementAdd');
    }

    /**
     * Notes:文章属性修改
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    public function elementUpdate(){
        $param = Fun::input();
        $this->elementSet($param, 'elementUpdate');
    }

    /**
     * Notes: 文章属性添加 / 修改
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    private function elementSet($param, $tmpl){
        $subFlag = $param['subFlag'];  // 数据提交来源标识
        if($subFlag == 'Y'){
            $essaySer = new Admin_EssayServe();
            $result = $essaySer->elementSet($param, $tmpl);
            Fun::json($result, true);
        }
        $setData['mouldCode'] = $param['m'];
        $setData['essayId'] = $param['i'];
        $setData['essayField'] = $param['f'];
        $setData['tmplId'] = $param['tmpl_id'];
        $commM = new Admin_CommonModel();
        $setData['tmplRule'] = $commM->getEssayInfo('element_tmpl', array('id'=>$setData['tmplId']));
        if($tmpl == 'elementUpdate'){
            $where['tpl_id'] = $setData['tmplId'];
            $where['essay_id'] = $setData['essayId'];
            $where['essay_field'] = $setData['essayField'];
            $setData['tmplInfo'] = $commM->getEssayInfo($setData['mouldCode'].'_list', $where);
        }
        // 映射
        foreach($setData as $k => $v){
            $this->set($k, $v);
        }
        $this->display($tmpl);
    }

}